<?php

namespace app\api\controller;

use addons\qrcode\Qrcode;
use app\common\controller\Api;
use think\Cache;
use think\Config;
/**
 * 个人中心接口
 */
class Person extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    //城市合伙人
    public function addCityMan(){
        $user = $this->auth->getUser();
        $name = input('name')?input('name'):'';
        if(!$name){
            $this->error('请输入姓名');
        }
        $phone = input('phone')?input('phone'):'';
        if(!$phone){
            $this->error('请输入电话号码');
        }
        $city = input('city')?input('city'):'';
        if(!$city){
            $this->error('请选择城市');
        }
        $note = input('note')?input('note'):'';
        $data['user_id'] = $user->id;
        $data['name'] = $name;
        $data['phone'] = $phone;
        $data['city'] = $city;
        $data['note'] = $note;
        $data['createtime'] = time();
        $res = db('cityman')->insert($data);
        if($res){
            $this->success('提交成功');
        }else{
            $this->error('提交失败');
        }
    }
    //ST管理页面
    public function mySteam(){
        $user = $this->auth->getUser();
        //自己的ST
        $where['user_id'] = $user->id;
        $where['status'] = 1;
        $myst = db('user_mill')->where($where)->sum('price');

        //团队ST
        $where = [];
        $where['tree'] = ['like','%,'.$user->id.',%'];
        $team = db('user')->where($where)->column('id');
        $where = [];
        $where['user_id'] = ['in',$team];
        $where['status'] = 1;
        $teamst = db('user_mill')->where($where)->sum('price');
        $data['myst'] = $myst;
        $data['teamst'] = $teamst;
        $this->success('ok',$data);
    }
    //我的ST列表
    public function mystList(){
        $user = $this->auth->getUser();
        $page = input('page')?input('page'):1;
        $limit = input('limit')?input('limit'):10;
        //自己的ST
        $where['user_id'] = $user->id;
        $where['status'] = 1;
        $myst = db('user_mill')->where($where)->page($page,$limit)->select();
        foreach($myst as $k=>$v){
            $mill = db('cb_mill')->where(['id'=>$v['cb_mill_id']])->find();
            $myst[$k]['mill_name'] = $mill['name'];
            $myst[$k]['createtime'] = date('Y-m-d',$v['createtime']);
        }
        $this->success('ok',$myst);
    }
    //团队ST列表
    public function teamstList(){
        $user = $this->auth->getUser();
        $page = input('page')?input('page'):1;
        $limit = input('limit')?input('limit'):10;
        //自己的ST
        $where = [];
        $where['tree'] = ['like','%,'.$user->id.',%'];
        $team = db('user')->where($where)->column('id');
        $where = [];
        $where['user_id'] = ['in',$team];
        $where['status'] = 1;
        $teamst = db('user_mill')->where($where)->page($page,$limit)->select();
        foreach($teamst as $k=>$v){
            $mill = db('cb_mill')->where(['id'=>$v['cb_mill_id']])->find();
            $user = db('user')->where(['id'=>$v['user_id']])->find();
            $teamst[$k]['mill_name'] = $mill['name'];
            $teamst[$k]['mobile'] = substr_replace($user['mobile'],'****',3,4);
            $teamst[$k]['createtime'] = date('Y-m-d',$v['createtime']);
        }
        $this->success('ok',$teamst);
    }
    //CB余额管理页面
    public function mycbnum(){
        $user = $this->auth->getUser();
        $where['id'] = $user->id;
        $user = db('user')->where($where)->find();
        $data['cb_num'] = round($user['cb_num']+$user['cb_freeze'],2);
        //获取最新价格
        $price = db('cb_price')->order('id desc')->find();
        if(!$price){
            $price['price'] = 0;
        }
        $to_cny = Config::get('site.to_cny');
        $data['cb_cny'] = round($data['cb_num']*$price['price']*$to_cny/100,2);
        $cb_trade = Config::get('site.cb_trade');
        $data['cb_trade'] = $cb_trade;
        $this->success('ok',$data);
    }
    //CC余额管理页面
    public function myccnum(){
        $user = $this->auth->getUser();
        $where['id'] = $user->id;
        $user = db('user')->where($where)->find();
        $data['cc_num'] = round($user['cc_num']+$user['cc_freeze'],2);
        //获取最新价格
        $price = db('cc_price')->order('id desc')->find();
        if(!$price){
            $price['price'] = 0;
        }
        $to_cny = Config::get('site.to_cny');
        $data['cc_cny'] = round($data['cc_num']*$price['price']*$to_cny/100,2);
        $cc_trade = Config::get('site.cc_trade');
        $data['cc_trade'] = $cc_trade;
        $this->success('ok',$data);
    }
    //实名认证
    public function setRealname(){
        $user = $this->auth->getUser();
        $truename = input('truename')?input('truename'):'';
        $pay_image = input('pay_image') ? input('pay_image') : '';
        if(!$truename){
            $this->error('请输入真实姓名');
        }
        $card_num = input('card_num')?input('card_num'):'';
        if(!$card_num){
            $this->error('请输入身份证号码');
        }
        $zheng_image = input('zheng_image') ? input('zheng_image') : $this->error('请上传身份证正面');
        $fan_image = input('fan_image') ? input('fan_image') : $this->error('请上传身份证正面');
        $need = db('config')->where('name','is_prepare')->value('value');
        if ($need == 1){
            //实名认证时,预排期正在进行
            $need = false;
        }else{
            //预排期结束
            $need = true;
            if (!$pay_image){
                $this->error('请上传付款凭证');
            }
        }
        $where['user_id'] = $user->id;
        $where['status'] = ['in','1,2'];
        $check = db('user_realname')->where($where)->find();
        if(!$check){
            //
            //实名认证对接

            //
            $data['zheng_image'] = $zheng_image;
            $data['fan_image'] = $fan_image;
            $data['user_id'] = $user->id;
            $data['truename'] = $truename;
            $data['card_num'] = $card_num;
            $data['status'] = 1;
            $data['createtime'] = time();
            $data['pay_image'] = $pay_image;
            $res = db('user_realname')->insert($data);
        }else{
            $this->error('请不要重复认证');
            if($check==2){
                $this->error('实名认证已通过，不用重复认证');
            }
            $data['truename'] = $truename;
            $data['card_num'] = $card_num;
            $data['status'] = 1;
            $data['createtime'] = time();
            $res = db('user_realname')->where($where)->update($data);
        }
        if($res){
            $this->success('提交成功');
        }else{
            $this->error('提交失败');
        }
    }
    //在线留言
    public function addOnlineMsg(){
        $user = $this->auth->getUser();
        $content = input('content')?input('content'):'';
        if(!$content){
            $this->error('请输入留言内容');
        }
        $image = input('image')?input('image'):'';
        if(!$image){
            $this->error('请上传图片');
        }
        $data['content'] = $content;
        $data['images'] = trim($image,',');
        $data['user_id'] = $user->id;
        $data['createtime'] = time();
        $res = db('online_msg')->insert($data);
        if($res){
            $this->success('留言成功');
        }else{
            $this->error('留言失败');
        }
    }
    //修改登录密码
    public function editLoginPass(){
        $user = $this->auth->getUser();
        $phone = input('phone')?input('phone'):'';
        if(!$phone){
            $this->error('请传手机号');
        }
        $password = input('password')?input('password'):'';
        if(!$password){
            $this->error('请输入新密码');
        }
        $repassword = input('repassword')?input('repassword'):'';
        if(!$repassword){
            $this->error('请输入确认密码');
        }
        if($password!=$repassword){
            $this->error('两次密码不一致');
        }
        $code = input('code')?input('code'):'';
        if(!$code){
            $this->error('请输入验证码');
        }
        $truecode = Cache::get($phone);
        if($code!=$truecode){
            $this->error('验证码错误');
        }
        $where['id'] = $user->id;
        $user = db('user')->where($where)->find();
        $data['password'] = md5(md5($password).$user['salt']);
        $res = db('user')->where($where)->update($data);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }

    }
    //修改支付密码
    public function editPayPass(){
        $user = $this->auth->getUser();
        $phone = input('phone')?input('phone'):'';
        if(!$phone){
            $this->error('请传手机号');
        }
        $password = input('password')?input('password'):'';
        if(!$password){
            $this->error('请输入新密码');
        }
        $repassword = input('repassword')?input('repassword'):'';
        if(!$repassword){
            $this->error('请输入确认密码');
        }
        if($password!=$repassword){
            $this->error('两次密码不一致');
        }
        $code = input('code')?input('code'):'';
        if(!$code){
            $this->error('请输入验证码');
        }
        $truecode = Cache::get($phone);
        if($code!=$truecode){
            $this->error('验证码错误');
        }
        $where['id'] = $user->id;
        $user = db('user')->where($where)->find();
        $data['paypass'] = md5(md5($password).$user['salt']);
        $res = db('user')->where($where)->update($data);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }

    }
    //获取收款账户信息
    public function getMyPayMsg(){
        $user = $this->auth->getUser();
        $where['id'] = $user->id;
        $user = db('user')->where($where)->find();
        $data['bank_username'] = $user['bank_username'];
        $data['bank_mobile'] = $user['bank_mobile'];
        $data['bank_num'] = $user['bank_num'];
        $data['bank_name'] = $user['bank_name'];
        $data['alipay_num'] = $user['alipay_num'] ? $user['alipay_num']: '';
        $data['alipay_image'] = $user['alipay_image'];
        $data['wechat_image'] = $user['wechat_image'];
        $pay_num = Config::get('site.pay_num');
        $data['pay_num'] = $pay_num;
        $this->success('ok',$data);
    }
    //设置收款账户信息
    public function setMyPayMsg(){
        $user = $this->auth->getUser();
        $where['id'] = $user->id;
        $user = db('user')->where($where)->find();
        $bank_username = input('bank_username')?input('bank_username'):'';
        if($bank_username){
            $data['bank_username'] = $bank_username;
        }else{
            $this->error('请补全信息');
        }
        $bank_mobile = input('bank_mobile')?input('bank_mobile'):'';
        if($bank_mobile){
            $data['bank_mobile'] = $bank_mobile;
        }else{
            $this->error('请补全信息');
        }
        $bank_num = input('bank_num')?input('bank_num'):'';
        if($bank_num){
            $data['bank_num'] = $bank_num;
        }else{
            $this->error('请补全信息');
        }
        $bank_name = input('bank_name')?input('bank_name'):'';
        if($bank_name){
            $data['bank_name'] = $bank_name;
        }else{
            $this->error('请补全信息');
        }
        $alipay_num = input('alipay_num')?input('alipay_num'):'';
        if($alipay_num){
            $data['alipay_num'] = $alipay_num;
        }else{
            $this->error('请补全信息');
        }
        $alipay_image = input('alipay_image')?input('alipay_image'):'';
        if($alipay_image){
            $data['alipay_image'] = $alipay_image;
        }else{
            $this->error('请补全信息');
        }
        $wechat_image = input('wechat_image')?input('wechat_image'):'';
        if($wechat_image){
            $data['wechat_image'] = $wechat_image;
        }else{
            $this->error('请补全信息');
        }
        if(!$data){
            $this->error('未传入任何参数');
        }
        if(!$user['bank_username'] && !$user['bank_mobile'] && !$user['bank_num'] && !$user['bank_name'] && !$user['alipay_num'] && !$user['alipay_image'] && !$user['wechat_image']){
            // 第一次不扣钱
            $res = db('user')->where($where)->update($data);
        }else{
            //扣钱
            $pay_num = Config::get('site.pay_num');
            if($user['cb_num']<$pay_num){
                $this->error('可用CB不足');
            }
            db('user')->where($where)->setDec('cb_num',$pay_num);
            //流水
            setCbwater($user['id'],$pay_num,2,'修改收款信息');
            $res = db('user')->where($where)->update($data);
        }
        if($res){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }
    //CB流水
    public function cbwater(){
        $user = $this->auth->getUser();
        $where['user_id'] = $user->id;
        $page = input('page')?input('page'):1;
        $limit = input('limit')?input('limit'):10;
        $list = db('cb_water')
            ->where($where)
            ->order('id desc')
            ->page($page,$limit)
            ->select();
        foreach($list as $k=>$v){
            $list[$k]['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
        }
        $this->success('ok',$list);
    }
    //CC流水
    public function ccwater(){
        $user = $this->auth->getUser();
        $where['user_id'] = $user->id;
        $page = input('page')?input('page'):1;
        $limit = input('limit')?input('limit'):10;
        $list = db('cc_water')->where($where)->order('id desc')->page($page,$limit)->select();
        foreach($list as $k=>$v){
            $list[$k]['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
        }
        $this->success('ok',$list);
    }
    //我的战队
    public function myTeamList(){
        $user = $this->auth->getUser();
        $where['first_id'] = $user->id;
        $tui_num = db('user')->where($where)->count();
        $page = input('page')?input('page'):1;
        $limit = input('limit')?input('limit'):10;
        $zong = db('user')->where('tree','like','%,'.$user->id.',%')->count();
        $list = db('user')
            ->field('id,username,idnum,createtime,nickname,avatar')
            ->where('tree','like','%,'.$user->id.',%')
            ->page($page,$limit)
            ->select();
        foreach($list as $k=>$v){
            $list[$k]['createtime'] = date('Y/m/d',$v['createtime']);
            $list[$k]['active'] = db('user_mill')
                ->where('status','1')
                ->where('user_id',$v['id'])
                ->sum('price');//个人活跃值
            $sonson = db('user')
                ->where('tree','like','%,'.$v['id'].',%')
                ->column('id');
            $sonson[] = $v['id'];
            $list[$k]['tuan_active'] = db('user_mill')
                ->where('status','1')
                ->where('user_id','in',$sonson)
                ->sum('price');//团队活跃值
            $list[$k]['zhi_num'] = db('user')
                ->where('first_id',$v['id'])
                ->count();//直推人数
        }
        $data['list'] = [];
//        for ($i = 0; $i<4;$i++){
//            $list = array_merge($list,$list);
//        }
        $data['zong'] = $zong;
        $data['tui_num'] = $tui_num;
        $data['list'] = $list;

        $this->success('ok',$data);
    }
    //互转页面
    public function transfersUs(){
        $user = $this->auth->getUser();
        $where['id'] = $user->id;
        $user = db('user')->where($where)->find();
        $data['cb_num'] = round($user['cb_num']+$user['cb_freeze'],2);
        $data['cc_num'] = round($user['cc_num']+$user['cc_freeze'],2);
        $this->success('ok',$data);
    }
    //资产管理页面
    public function personalMsg(){
        $user = $this->auth->getUser();
        $where['id'] = $user->id;
        $user = db('user')->where($where)->find();
        //个人活跃值
        $where = [];
        $where['user_id'] = $user['id'];
        $where['status'] = 1;
        $myst = db('user_mill')->where($where)->sum('price');

        //团队ST
        $where = [];
        $where['tree'] = ['like','%,'.$user['id'].',%'];
        $team = db('user')->where($where)->column('id');
        $where = [];
        $where['user_id'] = ['in',$team];
        $where['status'] = 1;
        $teamst = db('user_mill')->where($where)->sum('price');
        $data['myst'] = $myst;
        $data['teamst'] = $teamst;
        $data['cb_num'] = round($user['cb_num']+$user['cb_freeze'],2);
        $data['cc_num'] = round($user['cc_num']+$user['cc_freeze'],2);
        $data['avatar'] = $user['avatar']? $user['avatar'] : db('config')->where('name','app_logo')->value('value');
        $data['idnum'] = $user['idnum'];
        $data['mobile'] = $user['mobile'];
        $data['level'] = $user['level'];
        $data['is_truename'] = $user['is_truename'];
        $this->success('ok',$data);
    }
    //规则说明
    public function rulesmsg(){
        $data['play_rules'] = Config::get('site.play_rules');
        $url = $this->request->domain();
        $data['play_rules'] = str_replace('src="','stype=width:100% src="'.$url,$data['play_rules']);
        $this->success('ok',$data);
    }
    //分享链接
    public function shareurls(){
        $user = $this->auth->getUser();
        $where['id'] = $user->id;
        $user = db('user')->where($where)->find();
        $data['idnum'] = $user['idnum'];
        $url = $this->request->domain();
        $data['shareurl'] = $url.'/register#/?idnum='.$user['idnum'];
        $data['codeurl'] = $url.'/qrcode/build?text='.urlencode($url.'/register#/?idnum='.$user['idnum']);
        $this->success('ok',$data);
    }
}
